package com.client.qiuqiu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Timer;
import java.util.TimerTask;

public class ConnectServer extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connect_server);

        Connect_Thread Connect_thread = new Connect_Thread();
        Connect_thread.start();

        //跳转登录页
        Intent intent = new Intent (this, Login_page.class);
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                //跳转
                startActivity(intent);
                finish();
            }
        };
        timer.schedule(timerTask,2000);
    }


    class Connect_Thread extends Thread{
       public void run(){
           //在服务端ipconfig，拿到ip填入
           String serverIP = "192.168.1.102";
           SocketAddress addr = new InetSocketAddress(serverIP, 80);
           Socket socket = new Socket();
           {
               try {
                   socket.connect(addr, 1000);
                   socket.setSoTimeout(1000);
               } catch (IOException e) {
                   e.printStackTrace();
                   System.exit(0);
               }
           }

           //记录socket
           User user = ((User)getApplicationContext());
           user.setSocket(socket);

           //获取服务器端的输出流
           PrintStream printStream = null;
           try {
               printStream = new PrintStream(user.getSocket().getOutputStream());
           } catch (IOException e) {
               e.printStackTrace();
           }

           //发送socket类型
           //客户端与服务器通讯socket
           String msg = "1";
           printStream.println(msg);

           user.serverIP = serverIP;

       }
    }

}